
# this file is a collection of various channels for simulation
# purpose.
# 1. Binary symmetric channel.
# 2. Binary erasure channel.

# binary symmetric channel (BSC).
# S: the list of bits to be transmitted
# P: the crossover probability
bsc := proc(S, P)
local PTable, v, size, noiseVector;
    PTable := [P, 1-P];
    v := Statistics:-RandomVariable(ProbabilityTable(PTable));
    size := nops(S);
    noiseVector := convert(Statistics:-Sample(v, size) mod 2, list);
    (S + noiseVector) mod 2
end proc;

# binary erasure channel (BEC).
# S: the list of bits to be transmitted
# P: the erasure probability
bec := proc(S, P)
local PTable, v, R, size, noiseVector, i;
    PTable := [P, 1-P];
    v := Statistics:-RandomVariable(ProbabilityTable(PTable));
    size := nops(S);
    noiseVector := convert(Statistics:-Sample(v, size), list);
    R := [];
    for i from 1 to size do
        if noiseVector[i] = 1 then
            R := [op(R), e];
        else
            R := [op(R), S[i]];
        end if;
    od;
    R
end proc;
